package com.example.myapplication.Adapter;


import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.bean.Weather15DayBean;
import com.example.myapplication.bean.WeatherBean;
import com.example.myapplication.untils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private ArrayList<WeatherBean> list;
    private Integer pressure;
    //体感温度
    private String realFeel;
    private TextView textView2;

    public RecyclerAdapter(ArrayList<WeatherBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        holder.name.setText(huoyingList.get(position).getName());
//        holder.number.setText(String.valueOf(huoyingList.get(position).getNumber()));
//        holder.trump.setText(huoyingList.get(position).getTrump());
        holder.TvUvi.setText(list.get(position).getUvi());
        holder.TvVis.setText(list.get(position).getVis());
        String sunRise = list.get(position).getSunRise().substring(10);
        String sunSet = list.get(position).getSunSet().substring(10);
        textView2.setText(list.get(position).getUpdatetime());
        holder.TvSunRise.setText(sunRise);
        holder.TvSunSet.setText(sunSet);

        holder.TvWindDir.setText(list.get(position).getWindDir());
        holder.TvWindLevel.setText(list.get(position).getWindLevel());
        holder.TvWindSpeed.setText(list.get(position).getWindSpeed());
        pressure = Integer.valueOf(list.get(position).getPressure());
        realFeel = list.get(position).getRealFeel();



        List<Weather15DayBean> weather15DayBeans = list.get(position).getWeather15DayBeans();

        holder.rv_successive_dynasties_weather2.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
        holder.rv_successive_dynasties_weather2.setAdapter(new RecyclerAdapter2(weather15DayBeans,holder.itemView.getContext()));


    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView TvUvi;
        private TextView TvVis;
        private TextView TvSunRise;
        private TextView TvSunSet;
        private TextView TvWindDir;
        private TextView TvWindLevel;
        private TextView TvWindSpeed;


        RecyclerView rv_successive_dynasties_weather2;

        private WebView mywebview;
        private WebView mywebview2;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            TvUvi = (TextView) itemView.findViewById(R.id.Tv_uvi);
            TvVis = (TextView) itemView.findViewById(R.id.Tv_vis);
            TvSunRise = (TextView) itemView.findViewById(R.id.Tv_sunRise);
            TvSunSet = (TextView) itemView.findViewById(R.id.Tv_sunSet);
            textView2 = (TextView) itemView.findViewById(R.id.textView2);
            TvWindDir = (TextView) itemView.findViewById(R.id.Tv_windDir);
            TvWindLevel = (TextView) itemView.findViewById(R.id.Tv_windLevel);
            TvWindSpeed = (TextView) itemView.findViewById(R.id.Tv_windSpeed);
            rv_successive_dynasties_weather2 = itemView.findViewById(R.id.rv_successive_dynasties_weather2);
            this.mywebview = (WebView) itemView.findViewById(R.id.mywebview);
            this.mywebview2 = (WebView) itemView.findViewById(R.id.mywebview2);


            extracted(mywebview, "gauge-stage");
            extracted(mywebview2, "gauge2-temperature");


            mywebview.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (newProgress == 100) {
                        mywebview.loadUrl("javascript:toAndroidCall(" + pressure + ");");
                    }
                    super.onProgressChanged(view, newProgress);
                }
            });

            mywebview2.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (newProgress == 100) {
                        mywebview2.loadUrl("javascript:toAndroidCall(" + realFeel + ");");
                    }
                    super.onProgressChanged(view, newProgress);
                }
            });
        }

        @SuppressLint("SetJavaScriptEnabled")
        private WebView extracted(WebView mywebview, String url) {
            mywebview.loadUrl("file:///android_asset/" + url + ".html");
            mywebview.getSettings().setAllowFileAccess(true);
            mywebview.getSettings().setAllowContentAccess(true);
            mywebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            //设置js可以直接打开窗口，如window.open()，默认为false
            mywebview.getSettings().setJavaScriptEnabled(true);
            //是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
            mywebview.getSettings().setSupportZoom(true);
            //是否可以缩放，默认true
            mywebview.getSettings().setBuiltInZoomControls(false);
            // 设置此属性，可任意比例缩放。大视图模式
            mywebview.getSettings().setLoadWithOverviewMode(true);
            return mywebview;
        }

    }
}